package client;

/**
 * Created by Levin Alexander
 * and Skryabina Tatyana on 25.11.14.
 */
public class Proxy {
    private final String SERVER_TERMINATE = "SERVER TERMINATE";
    private final Transport transport;

    public Proxy(Transport transport) {
        this.transport = transport;
    }

    /**
     * Send message to the server. If user can't send, method tried to stop any work.
     *
     * @param message for sending
     */
    public void send(String message) {
        try {
            transport.request(message);
        } catch (TransportException e) {
            transport.closeSocket();
            System.out.println("Try connect later.");
        }
    }

    /**
     * Read message from the server. If user can't get response (or response is null), try to stop any work.
     */
    public boolean read() {
        try {
            String response = transport.response();

            if (!SERVER_TERMINATE.equals(response)) {
                System.out.println(response);
                return true;
            } else {
                transport.closeSocket();
            }

        } catch (TransportException e) {
            transport.closeSocket();
            System.out.println("Try connect later.");
        }
        return false;
    }
}
